package com.xiaoxing.zhinenghuacangku_liaocheng.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoxing.zhinenghuacangku_liaocheng.R;
import com.xiaoxing.zhinenghuacangku_liaocheng.R2;
import com.xiaoxing.zhinenghuacangku_liaocheng.di.component.DaggerLiaoCheng_TuiKuChuKuDanComponent;
import com.xiaoxing.zhinenghuacangku_liaocheng.di.module.LiaoCheng_TuiKuChuKuDanModule;
import com.xiaoxing.zhinenghuacangku_liaocheng.mvp.contract.LiaoCheng_TuiKuChuKuDanContract;
import com.xiaoxing.zhinenghuacangku_liaocheng.mvp.presenter.LiaoCheng_TuiKuChuKuDanPresenter;
import com.xiaoxing.zhinenghuacangku_liaocheng.mvp.ui.adapter.LiaoCheng_TuiKuChuKuDanAdapter;
import com.xiaoxing.zhinenghuacangku_liaocheng.mvp.ui.entity.LiaoCheng_TuiKuChuKuDan;
import com.xw.repo.XEditText;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import com.jess.arms.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonres.view.RecycleViewDivider;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ZHI_NENG_HUA_CANG_KU_LIAO_CHENG_ACTIVITY_LIAO_CHENG_TUI_KU_CHU_KU_DAN)
public class LiaoCheng_TuiKuChuKuDanActivity extends BaseActivity<LiaoCheng_TuiKuChuKuDanPresenter> implements LiaoCheng_TuiKuChuKuDanContract.View, OnRefreshListener {


    private static boolean mIsNeedDemo = true;
    @BindView(R2.id.empty_text)
    TextView empty_text;
    @BindView(R2.id.empty_image)
    ImageView empty_image;

    @BindView(R2.id.empty)
    View mEmptyLayout;
    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refreshLayout)
    RefreshLayout mRefreshLayout;
    @BindView(R2.id.xet_jie_shou_ren_xing_ming)
    XEditText xetJieShouRenXingMing;
    @BindView(R2.id.xet_jie_shou_ren_dian_hua)
    XEditText xetJieShouRenDianHua;
    @BindView(R2.id.xet_wu_liao_ming_cheng)
    XEditText xetWuLiaoMingCheng;
    @BindView(R2.id.xet_xiang_mu_ming_cheng)
    XEditText xetXiangMuMingCheng;
    @BindView(R2.id.btn_cha_xun)
    Button btnChaXun;
    private LiaoCheng_TuiKuChuKuDanAdapter mAdapter;
    private List<LiaoCheng_TuiKuChuKuDan> mDataBeanList = new ArrayList<>();


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLiaoCheng_TuiKuChuKuDanComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .liaoCheng_TuiKuChuKuDanModule(new LiaoCheng_TuiKuChuKuDanModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_liao_cheng__tui_ku_chu_ku_dan; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_liao_cheng__tui_ku_chu_ku_dan));

        initRefreshLayout();
        initRecyclerView();
        initEmpty();
    }


    private void initEmpty() {
        empty_image.setImageResource(R.drawable.ic_empty);
        empty_text.setTextColor(Color.parseColor("#D8D8D8"));
        empty_text.setText("暂无数据");
    }


    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(LiaoCheng_TuiKuChuKuDanActivity.this));
        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 10, Color.parseColor("#EEEEEE")));

        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new LiaoCheng_TuiKuChuKuDanAdapter(LiaoCheng_TuiKuChuKuDanActivity.this, mDataBeanList));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("type", "1");
                bundle.putString("mid", mDataBeanList.get(position).getMid());
                Utils.navigation(getContext(), RouterHub.ZHI_NENG_HUA_CANG_KU_LIAO_CHENG_ACTIVITY_LIAO_CHENG_SAO_MA_TUI_KU, bundle);
            }
        });
    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new
                ClassicsHeader(this).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.autoRefresh();
        mRefreshLayout.setEnableLoadMore(false);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        //getLiaoCheng_TuiKuChuKuDanList();
        mRefreshLayout.finishRefresh();
        mEmptyLayout.setVisibility(View.GONE);

    }

    private void getOutStoreListLiaoChengForReturn() {
        showLoading();

        String methodName = "GetOutStoreListLiaoChengForReturn";
        SoapParams params = new SoapParams();
        params.put("username", xetJieShouRenXingMing.getText().toString().trim());
        params.put("phone", xetJieShouRenDianHua.getText().toString().trim());
        params.put("material", xetWuLiaoMingCheng.getText().toString().trim());
        params.put("projectname", xetXiangMuMingCheng.getText().toString().trim());
        /**
         * 请求
         */
        String finalMethodName = methodName;
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(finalMethodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    try {
                        List<LiaoCheng_TuiKuChuKuDan> entityList = new Gson().fromJson(result, new TypeToken<List<LiaoCheng_TuiKuChuKuDan>>() {
                        }.getType());
                        if (entityList != null && entityList.size() > 0) {
                            mDataBeanList.clear();
                            mDataBeanList.addAll(entityList);
                            mAdapter.notifyDataSetChanged();
                            mEmptyLayout.setVisibility(View.GONE);
                            mRecyclerView.setVisibility(View.VISIBLE);
                        } else {
                            mEmptyLayout.setVisibility(View.VISIBLE);
                            mRecyclerView.setVisibility(View.GONE);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    public void getLiaoCheng_TuiKuChuKuDanDataSuccess(LiaoCheng_TuiKuChuKuDan entityList) {

//        if (entityList != null && entityList.getData() != null && entityList.getData().size() > 0) {
//            mDataBeanList.clear();
//            mDataBeanList.addAll(entityList.getData());
//            mAdapter.notifyDataSetChanged();
//            mEmptyLayout.setVisibility(View.GONE);
//            mRecyclerView.setVisibility(View.VISIBLE);
//        } else {
//            mEmptyLayout.setVisibility(View.VISIBLE);
//            mRecyclerView.setVisibility(View.GONE);
//        }

    }

    private void getLiaoCheng_TuiKuChuKuDanList() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getLiaoCheng_TuiKuChuKuDanList(map);
    }


    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog(this);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);

    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick(R2.id.btn_cha_xun)
    public void onViewClicked() {
        mDataBeanList.clear();
        mAdapter.notifyDataSetChanged();
        mEmptyLayout.setVisibility(View.VISIBLE);
        mRecyclerView.setVisibility(View.GONE);
        getOutStoreListLiaoChengForReturn();
    }
}
